package com.blog.web;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.blog.dao.BlogDAO;
import com.blog.dao.TypeDAO;
import com.blog.domain.Blog;
import com.blog.domain.User;
import com.blog.service.BlogService;

@Controller
public class BlogController {
	
	@Autowired
	private BlogService blogService;

	@Autowired
	private BlogDAO blogDAO;
	
	@Autowired
	private TypeDAO typeDAO;
	
	public BlogDAO getBlogDAO() {
		return blogDAO;
	}

	public void setBlogDAO(BlogDAO blogDAO) {
		this.blogDAO = blogDAO;
	}
	
	public TypeDAO getTypeDAO() {
		return typeDAO;
	}

	public void setTypeDAO(TypeDAO typeDAO) {
		this.typeDAO = typeDAO;
	}

	public BlogService getBlogService() {
		return blogService;
	}

	public void setBlogService(BlogService blogService) {
		this.blogService = blogService;
	}
	
	@RequestMapping("/addBlog")
	public String addBlog(@ModelAttribute Blog blog, HttpServletRequest request) {
		blog.setReader(0);
		blog.setUser((User)request.getSession().getAttribute("user"));
		blogService.saveBlog(blog);
		return "redirect:/#blog_list";
	}
	
	@RequestMapping("/input")
	public String input() {
		System.out.println("invoke");
		return "input";
	}
	
	@RequestMapping("/blog/{id}")
	public ModelAndView blog(@PathVariable int id) {
		Blog blog = blogService.findBlogByPrimaryKey(id);
		if(blog != null) {
			blog.setReader(blog.getReader() + 1);
			blogService.saveBlog(blog);
			return new ModelAndView("blog", "blog", blog);
		}
		return new ModelAndView("404");
	}
	
	@RequestMapping("/search")
	public ModelAndView search(@RequestParam String content) throws UnsupportedEncodingException {
		/* 
		 * 浏览传过来的中文默认为iso-8859-1编码，由于该编码不支持中文
		 * 所以将其转成utf-8格式	 
		 */
		content = new String(content.getBytes("iso-8859-1"), "utf-8");
		/**
		 * 将关键字分别匹配标题跟内容进行模糊查询
		 * 将查询结果合并，返回
		 */
		Set<Blog> list = blogDAO.findBlogByTitleContaining("%" + content + "%");
		List<Blog> list2 = blogDAO.executeQuery("from Blog where content like ?", "%" + content + "%");
		list.addAll(list2);
		return new ModelAndView("blog_list", "blog_list", list);
	}
	
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable int id) {
		Blog blog = blogService.findBlogByPrimaryKey(id);
		if (blog != null) {
			blogService.deleteBlog(blog);
		}
		return "redirect:/#blog_list";
	}
	
	@RequestMapping("/blog_list")
	public ModelAndView blog_list() {
		List<Blog> blog_list = blogDAO.executeQuery("from Blog order by id desc", 0, 10);
		return new ModelAndView("blog_list", "blog_list", blog_list);
	}
	
	
	
	
	
	
	
	
	
	
}
